package com.cmc.cloud.cmclink.doc.rpcservice.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.cmc.cloud.cmclink.doc.constants.errorconstant.SoErrorCodeConstants;
import com.cmc.cloud.cmclink.doc.rpcservice.IbsMgmtService;
import com.cmc.cloud.cmclink.ibsmgmt.api.mgmt.EnterpriseTopAgentApi;
import com.cmc.cloud.cmclink.ibsmgmt.api.mgmt.dto.TopAgentReqDTO;
import com.cmc.cloud.cmclink.ibsmgmt.api.mgmt.dto.TopAgentSettingRespDTO;
import com.cmes.framework.common.exception.util.ServiceExceptionUtil;
import com.cmes.framework.common.pojo.CommonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;

/**
 * 箱型基础信息
 *
 * @author dev89
 * @date 2025/08/05 13:37
 **/
@Service
@Validated
@Slf4j
public class IbsMgmtServiceImpl implements IbsMgmtService {
    @Resource
    EnterpriseTopAgentApi enterpriseTopAgentApi;
    private static final String IBS_BOOKING_IDENTITY = "IBS_BOOKING_IDENTITY";

    public TopAgentSettingRespDTO getTopAgentByCsmCodeAndPol(String csmCode, String polCode) {
        TopAgentReqDTO param = new TopAgentReqDTO();
        //订舱代理人:IBS_BOOKING_IDENTITY 结算人:SETTLER
        param.setCsmCode(csmCode);
        param.setPortCode(polCode);
        param.setType(IBS_BOOKING_IDENTITY);
        CommonResult<List<TopAgentSettingRespDTO>> commonResult;
        try {
            commonResult = enterpriseTopAgentApi.listEnterpriseTopAgent(param);
        } catch (Exception e) {
            throw ServiceExceptionUtil.exception(SoErrorCodeConstants.SO_TOP_AGENT_ERROR);
        }
        // 只会有一个匹配
        if (CollectionUtil.isNotEmpty(commonResult.getCheckedData())) {
            return commonResult.getCheckedData().get(0);
        }
        return null;
    }

}
